#-*- coding:UTF-8 -*-
"""
controller: SyntaxHighlighter
All rights reserved
"""
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import memcache
from google.appengine.runtime import apiproxy_errors
import os
import logging
import random
import time

from views import NotFound

class MainPage(webapp.RequestHandler):
    def post(self):
        SupportType={"Bash":"bash",
                     "CSharp":"csharp",
                     "Cpp":"cpp",
                     "Css":"css",
                     "Delphi":"delphi",
                     "Diff":"diff",
                     "Groovy":"groovy",
                     "JScript":"js",
                     "Java":"java",
                     "Perl":"perl",
                     "Php":"php",
                     "Plain":"text",
                     "Python":"python",
                     "Ruby":"ruby",
                     "Scala":"scala",
                     "Sql":"sql",
                     "Vb":"vb",
                     "Xml":"xml"
                     }
        bush=self.request.get("type")
        resultstr="""<pre class="brush: %s">\n"""%SupportType.get(bush)
        resultstr+=self.request.get("code")
        resultstr+="\n</pre>"
        bushscript="""<script type="text/javascript" src="/js/shBrush%s.js"></script>"""%bush
        template_values={
                         "SupportType":SupportType,
                         "input":False,
                         "result":resultstr,
                         "bushscript":bushscript
                         }
        path = os.path.join(os.path.dirname(__file__), 'templates/SyntaxHighlighter.html')
        self.response.out.write(template.render(path,template_values))        
    def get(self):
        SupportType={"Bash":"bash",
                     "CSharp":"csharp",
                     "Cpp":"cpp",
                     "Css":"css",
                     "Delphi":"delphi",
                     "Diff":"diff",
                     "Groovy":"groovy",
                     "JScript":"js",
                     "Java":"java",
                     "Perl":"perl",
                     "Php":"php",
                     "Plain":"text",
                     "Python":"python",
                     "Ruby":"ruby",
                     "Scala":"scala",
                     "Sql":"sql",
                     "Vb":"vb",
                     "Xml":"xml"
                     }
        template_values={
                         "SupportType":SupportType,
                         "input":True
                         }
        path = os.path.join(os.path.dirname(__file__), 'templates/SyntaxHighlighter.html')
        self.response.out.write(template.render(path,template_values))


def main():
    application = webapp.WSGIApplication([
                                          ('/SyntaxHighlighter/?',MainPage),
                                          ('/SyntaxHighlighter',MainPage),
                                          ('.*',NotFound)
                                          ],debug=True)
    run_wsgi_app(application)

if __name__ == "__main__":
    main()